Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
"Unresolved function or method" with Passport.JS

After setting up passport.js and mongoose authentication, I am consistently getting the "Unresolved function or method" on all of my relevant statements.

enter image description here enter image description here

I tried invalidating chaches and restarting along with other workarounds, however, the problem has persisted.

My user.js:

var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');

//create user schema and model
var User = new Schema({
    username: String,
    password: String,
    studentID: String,
    grades: [{
        subject: String,
        grade: String
    }]
});

User.plugin(passportLocalMongoose);

module.exports = mongoose.model('user', User);

When I run the server, I also get the error:

TypeError: passport.serialize is not a function

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

user.js

const mongoose = require('mongoose');
const bcrypt = require('bcrypt-nodejs');

const Schema = mongoose.Schema;

//= ===============================
// User Schema
//= ===============================
const UserSchema = new Schema({
  email: {
    type: String,
    lowercase: true,
    unique: true,
    required: true
  },
  password: {
    type: String,
    required: true
  }
  });


// Pre-save of user to database, hash password if password is modified or new
UserSchema.pre('save', function (next) {
  const user = this,
    SALT_FACTOR = 5;

  if (!user.isModified('password')) return next();

  bcrypt.genSalt(SALT_FACTOR, (err, salt) => {
    if (err) return next(err);

    bcrypt.hash(user.password, salt, null, (err, hash) => {
      if (err) return next(err);
      user.password = hash;
      next();
    });
  });
});

// Method to compare password for login
UserSchema.methods.comparePassword = function (candidatePassword, cb) {
  bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
    if (err) { return cb(err); }

    cb(null, isMatch);
  });
};

module.exports = mongoose.model('User', UserSchema);

passport.js

const passport = require('passport');
const User = require('../models/user');
const config = require('./main');
const LocalStrategy = require('passport-local');

// username field is now email
const localOptions = {
  usernameField: 'email'
};

// set up the local login strategy
const localLogin = new LocalStrategy(localOptions, (email, password, done) => {
  User.findOne({ email }, (err, user) => {
    if (err) { return done(err); }
    if (!user) { return done(null, false, { error: 'Your login details could not be verified. Please try again.' }); }

    user.comparePassword(password, (err, isMatch) => {
      if (err) { return done(err); }
      if (!isMatch) { return done(null, false, { error: 'Your login details could not be verified. Please try again.' }); }

      return done(null, user);
    });
  });
});

passport.use(localLogin);
over 4 years ago · Santiago Trujillo Relatório

0

So, now, you just need to create a controller to handle the business logic for user registrations and so on. For example,

* UserController.js *

// other functions

// register
export function register() {
  if (!req.body) {
    res.status(500).json({ error: 'All Fields Required.' });
  }

  const user = new User(req.body)

  user.save((err, user) => {
    if (err) {
      res.status(500).json({ err });
    }
    res.status(200).json({ user });
  })
}

router.js

// other important things to require
const passportService = require('./config/passport');

// Middleware to require login/auth
const requireLogin = passport.authenticate('local', { session: false 
});

// your routes go here and you can access requireLogin now, like so:

router.get('/users/:id', requireLogin, getUserById);
router.post('/users', register);

Hope this helps. Not trying to make you change your codebase but this is a better way to do what you want to do.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda